Vector¶

Vector is base class of other vector like classes. It represents a series of vectors.

In [1]:
from py3d import Vector
Vector()
Out[1]:
Vector([], dtype=float64)
In [3]:
Vector([0, 0, 0, 0])
Out[3]:
Vector([0, 0, 0, 0])
In [5]:
Vector([1, 2, -4])
Out[5]:
Vector([ 1,  2, -4])
In [7]:
Vector([1, 2, 3]).tile(2)
Out[7]:
Vector([[1, 2, 3],
        [1, 2, 3]])
In [9]:
import py3d
py3d.Vector([1, 2, 0, 3])
Out[9]:
Vector([1, 2, 0, 3])
In [10]:
Vector([1]).tile(2)
Out[10]:
Vector([[1],
        [1]])

Get unit vector

In [12]:
import py3d
V32 = py3d.Vector([
    [1, 3],
    [2, -9],
    [0.1, -3]])
V32.U
Out[12]:
Vector([[ 0.31622777,  0.9486833 ],
        [ 0.21693046, -0.97618706],
        [ 0.03331483, -0.99944491]])

Get length of vectors

In [13]:
V32.L
Out[13]:
array([3.16227766, 9.21954446, 3.0016662 ])
In [15]:
from numpy import allclose
assert allclose(V32.U.L, [[1], [1], [1]])

Get homogeneous vector

In [16]:
V32.H
Out[16]:
Vector3([[ 1. ,  3. ,  1. ],
         [ 2. , -9. ,  1. ],
         [ 0.1, -3. ,  1. ]])

Get a series of random vectors

In [18]:
import py3d
py3d.rand(2, 3, 4)
Out[18]:
Vector4([[[0.09026492, 0.72233889, 0.36505585, 0.97094683],
          [0.21166808, 0.12931903, 0.16788786, 0.51542843],
          [0.86873981, 0.33323856, 0.76010574, 0.70500161]],

         [[0.40365659, 0.4906894 , 0.76264576, 0.40212302],
          [0.46794375, 0.1845394 , 0.76276725, 0.5210683 ],
          [0.60283204, 0.03065583, 0.97249027, 0.89473353]]])
In [20]:
import py3d
py3d.Vector([[0, 0], [1, 2], [3, 4]]).diff()
Out[20]:
Vector([[1, 2],
        [2, 2]])

Get a flattened copy of a vector

In [22]:
import py3d
py3d.Vector([[[1,2,3]],[[4,5,6]]]).flatten()
Out[22]:
Vector([[1, 2, 3],
        [4, 5, 6]])

Fill nan elements of a Vector

In [24]:
import py3d
py3d.Vector([0, 1, float("nan")]).fillna(-1)
Out[24]:
Vector([ 0.,  1., -1.])

Drop duplicates

In [25]:
import py3d
py3d.Vector([
    [1,2,1],
    [2,3,4],
    [1,2,1],
    [5,3,4]
]).unique()
Out[25]:
Vector([[1, 2, 1],
        [2, 3, 4],
        [5, 3, 4]])

Read an image

In [26]:
import py3d
py3d.read_img("./20220917214012.jpg").as_image()
Out[26]:

Read a txt file storing poses

In [27]:
import py3d
py3d.read_txt("poses.txt")
Out[27]:
Vector([[ 1.00000000e+00, -3.80098575e-01, -9.24678213e-01, -2.22547866e-02,  8.46246797e+02, -9.23810709e-01,  3.80713880e-01, -4.03821316e-02,  3.72507563e+03,  4.58131834e-02,  5.21001950e-03, -9.98936438e-01,  1.15277604e+02],
        [ 9.00000000e+00, -3.34781384e-01, -9.41921870e-01, -2.65446075e-02,  8.46339549e+02, -9.41148328e-01,  3.35630805e-01, -3.98972180e-02,  3.72529806e+03,  4.64892502e-02,  1.16255671e-02, -9.98851138e-01,  1.15261281e+02],
        [ 1.40000000e+01, -2.92967993e-01, -9.55757956e-01, -2.63910066e-02,  8.46403792e+02, -9.55019148e-01,  2.93844004e-01, -3.99265447e-02,  3.72550887e+03,  4.59149518e-02,  1.35067170e-02, -9.98854036e-01,  1.15246581e+02],
        [ 2.20000000e+01, -2.55823106e-01, -9.66433871e-01, -2.36666831e-02,  8.46452174e+02, -9.65654193e-01,  2.56614723e-01, -4.07536888e-02,  3.72572334e+03,  4.54589646e-02,  1.24280965e-02, -9.98888895e-01,  1.15235256e+02],
        [ 3.00000000e+01, -2.21980891e-01, -9.74885116e-01, -1.79859637e-02,  8.46498002e+02, -9.74096121e-01,  2.22541934e-01, -4.01476730e-02,  3.72595586e+03,  4.31420000e-02,  8.60804130e-03, -9.99031866e-01,  1.15230213e+02],
        [ 3.50000000e+01, -1.89591125e-01, -9.81721142e-01, -1.66974492e-02,  8.46543252e+02, -9.80764734e-01,  1.90155814e-01, -4.40602123e-02,  3.72619118e+03,  4.64299590e-02,  8.02284410e-03, -9.98889330e-01,  1.15216815e+02],
        [ 5.30000000e+01, -1.52544592e-01, -9.88232558e-01, -1.12498162e-02,  8.46578518e+02, -9.87793431e-01,  1.52820235e-01, -3.01680937e-02,  3.72641248e+03,  3.15322920e-02,  6.51051500e-03, -9.99481530e-01,  1.15240333e+02],
        [ 5.70000000e+01, -1.08222204e-01, -9.94126669e-01, -3.45932600e-04,  8.46594185e+02, -9.93640561e-01,  1.08180154e-01, -3.12328284e-02,  3.72664364e+03,  3.10868107e-02, -3.03635290e-03, -9.99512076e-01,  1.15239078e+02],
        [ 6.10000000e+01, -5.47405853e-02, -9.98500181e-01, -9.25326500e-04,  8.46621932e+02, -9.98321631e-01,  5.47482953e-02, -1.88823863e-02,  3.72687760e+03,  1.89047262e-02, -1.09859400e-04, -9.99821284e-01,  1.15261382e+02],
        [ 6.50000000e+01,  2.32873930e-03, -9.99995499e-01,  1.89191590e-03,  8.46628861e+02, -9.99959160e-01, -2.34516750e-03, -8.72806030e-03,  3.72713067e+03,  8.73245790e-03, -1.87151330e-03, -9.99960120e-01,  1.15274254e+02]])

Write csv file

In [28]:
import py3d
py3d.Vector([[1,2,3],[4,5,6]], columns=["t","x","y"]).to_csv("with_cols.csv")
py3d.Vector([[7,8.4,9],[-1,-2,-3]]).to_csv("no_cols.csv")

Read csv file

In [29]:
import py3d
a=py3d.read_csv("with_cols.csv", header=1)
a["t"], a["t","x"], a[1,2]
Out[29]:
(Vector([1., 4.]),
 Vector([[1., 2.],
         [4., 5.]]),
 6.0)
In [30]:
import py3d
py3d.read_csv("no_cols.csv")
Out[30]:
Vector([[ 7. ,  8.4,  9. ],
        [-1. , -2. , -3. ]])

Read pcd file

In [31]:
import py3d
py3d.read_pcd("ascii.pcd").xyz.as_point(py3d.Color(r=1,g=0.5,a=0.3))
Out[31]:
In [32]:
import py3d
pcd = py3d.read_pcd("binary.pcd")
print("min", pcd.min())
print("max", pcd.max())
pcd.xyz.as_point(colormap=pcd.w)
min -109.0828628540039
max 1223933577.0853775
Out[32]:
In [33]:
pcd.sample(10)
Out[33]:
Vector([[ 7.39166689e+00, -6.02787781e+00, -1.19708133e+00,  1.19000000e+02,  1.22393358e+09],
        [-3.51005101e+00, -5.58581066e+00, -5.92625916e-01,  1.82000000e+02,  1.22393358e+09],
        [-7.09445429e+00, -7.74244595e+00,  2.80921161e-01,  1.78000000e+02,  1.22393358e+09],
        [-1.49296093e+01, -5.85207081e+00,  6.97743356e-01,  1.07000000e+02,  1.22393358e+09],
        [ 3.70436502e+00, -6.92269421e+00, -7.60660112e-01,  1.12000000e+02,  1.22393358e+09],
        [ 2.20531883e+01,  1.41763506e+01, -7.19668329e-01,  1.62000000e+02,  1.22393358e+09],
        [-1.66476345e+01, -3.65677786e+00, -1.65196347e+00,  1.07000000e+02,  1.22393358e+09],
        [-1.27265158e+01,  1.62192802e+01, -3.00196958e+00,  1.19000000e+02,  1.22393358e+09],
        [ 2.65575504e+00, -4.67713213e+00, -1.78518832e+00,  1.30000000e+02,  1.22393358e+09],
        [-1.85654449e+00,  1.37091408e+01, -1.39663410e+00,  1.82000000e+02,  1.22393358e+09]])

split the Vector into 3 parts

In [34]:
pcd.split([3,4])
Out[34]:
[Vector([[ -9.84686852,  25.20105171,  -2.90541101],
         [-11.15411758,  26.11715317,  -2.88157201],
         [-36.65405273,  72.06262207,   1.20190442],
         ...,
         [-10.16968441,  20.41918373,  -3.34819937],
         [ -5.50718451,  13.51468945,  -3.03626227],
         [ -6.40060711,  13.55139732,  -2.96159911]]),
 Vector([[164.],
         [176.],
         [215.],
         ...,
         [153.],
         [141.],
         [ 81.]]),
 Vector([[1.22393358e+09],
         [1.22393358e+09],
         [1.22393358e+09],
         ...,
         [1.22393358e+09],
         [1.22393358e+09],
         [1.22393358e+09]])]

Write npy file

In [35]:
import py3d
py3d.Vector([1,2,3]).to_npy("tmp.npy")

Read npy file

In [36]:
import py3d
py3d.read_npy("tmp.npy")
Out[36]:
Vector([1, 2, 3])

Read a real lidar point cloud from a npy file

In [37]:
import py3d
py3d.read_npy("lidar.npy").xyz.as_point()
Out[37]:

Read a depth map from a npy file

In [38]:
import py3d
py3d.read_npy("depth.npy").z.as_image()
Out[38]:

Read ply file

In [39]:
import py3d
vertices, mesh = py3d.read_ply("cube.ply")
print(vertices)
mesh
[[0. 0. 0.]
 [0. 0. 1.]
 [0. 1. 1.]
 [0. 1. 0.]
 [1. 0. 0.]
 [1. 0. 1.]
 [1. 1. 1.]
 [1. 1. 0.]]
Out[39]:
In [ ]: